package com.LeeCode;

/**
 * 每个小孩最多能分到多少糖果
 */

public class Code2226 {
    public static void main(String[] args) {
        int[] candies = {1, 2, 3, 4, 10};
        long k = 5;
        System.out.println(new Code2226().maximumCandies(candies, k));
        System.out.println(new Code2226().check(candies, 1, k));
    }

    public int maximumCandies(int[] candies, long k) {
        int left = 0, right = 0;
        for (int num : candies) {
            right = Math.max(right, num + 1);
        }

        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(candies, mid, k))
                left = mid;
            else
                right = mid;
        }
        return left;
    }

    private boolean check(int[] candies, int num, long k) {
        long total = 0;
        for (int candy : candies) {
            total += candy / num;
            if (total >= k)
                return true;
        }
        return false;
    }
}
